using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ATest.Bll.Repositories;
using ATest.Bll;
using ATest.Helpers;

namespace ATest.Controllers
{
    [HandleError]
    [Authorize]
    [ActionFilters.AjaxActionFilter]
    public class QuestionController : Controller
    {
        public QuestionController() : this(new QuestionRepository(), new TestRepository())
        {
        }

        public QuestionController(IQuestionRepository questionRepository, ITestRepository testRepository)
        {
            this._QuestionRepository = questionRepository;
            this._TestRepository = testRepository;
        }

        IQuestionRepository _QuestionRepository;
        ITestRepository _TestRepository;

        //
        // GET: /Question/

        public ActionResult Index(int testId, bool isAjax)
        {
            var test = _TestRepository.GetById(testId);
            if (test != null)
            {
                var model = new Models.QuestionListPresentation(test, _QuestionRepository.GetAllByTestId(test.Id).ToList());
                if (isAjax)
                    return PartialView("Ajax_Index", new Models.QuestionListPresentation(test, model));
                else
                    return View(model);
            }
            else
            {
                return View("NotFound");
            }
        }


        public ActionResult Details(int id, int testId, bool isAjax)
        {
            var data = _QuestionRepository.GetById(id);
            if (data != null && data.TestId == testId)
            {
                var model = new Models.QuestionPresentation(data.Test, data);
                if (isAjax)
                    return PartialView("Ajax_Details", model);
                else
                    return View(model);
            }
            else
            {
                return View("NotFound");
            }            
        }

        public ActionResult Edit(int id, int testId, bool isAjax)
        {
            var data = _QuestionRepository.GetById(id);
            if (data != null && data.TestId == testId)
            {
                var model = new Models.QuestionPresentation(data.Test, data);
                if (isAjax)
                    return PartialView("Ajax_Edit", model);
                else
                    return View(model);
            }
            else
            {
                return View("NotFound");
            }
        }

        public ActionResult Create(int testId, bool isAjax)
        {
            var test = _TestRepository.GetById(testId);
            if (test != null)
            {
                var model = new Models.QuestionPresentation(test, null);
                if (isAjax)
                {
                    return PartialView("Ajax_Create", model);
                }
                else
                {
                    return View(model);
                }
            }
            else
            {
                return View("NotFound");
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public object Create(int testId, string name, string text, bool isAjax)
        {
            var test = _TestRepository.GetById(testId);
            if (test != null)
            {
                var question = new Question();
                question.TestId = testId;
                question.Name = name;
                question.SetText(text);

                _QuestionRepository.Insert(question);

                try
                {
                    _QuestionRepository.SaveChanges();
                    if (isAjax)
                    {
                        return View("Details", question);
                    }
                    else
                    {
                        return RedirectToAction("Details", new { id = question.Id, testId = question.TestId });
                    }
                }
                catch (Bll.Errors.ValidationException)
                {
                    ModelState.AddModelErrors(question.GetRuleViolations());
                    var model = new Models.QuestionPresentation(test, question);
                    return View(model);
                }
            }
            else
            {
                return View("NotFound");
            }
        }
    }
}
